package com.rongshu.view.penpal.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.view.base.AdapterListener;
import com.rongshu.view.base.BaseFragment;
import com.rongshu.view.base.mvp.INetView;
import com.rongshu.view.discovery.bean.ArticleDetaisRequest;
import com.rongshu.view.home.activity.SingleArticleDetailsActivity;
import com.rongshu.view.penpal.activity.PostDetailsActivity;
import com.rongshu.view.penpal.adapter.PostItemAdapter;
import com.rongshu.view.penpal.bean.PenpalArticleResponse;
import com.rongshu.view.penpal.present.PenpalArticleListPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * created by zhengweis on 2018/11/22.
 * description：笔友会 文章/帖子列表
 */
public class Penpal_PostListFragment extends BaseFragment<PenpalArticleListPresenter> implements INetView {
    @BindView(R.id.article_list)
    RecyclerView articleList;
    @BindView(R.id.refresh_view)
    SmartRefreshLayout refreshView;
    @BindView(R.id.no_data_img)
    ImageView noData;
    @BindView(R.id.no_data_hint)
    TextView noDataHint;
    private int penpalwill_id;
    private int type;
    private int pageIndex = 1;
    private int pageSize = 10;
    private List<PenpalArticleResponse.ResponseBean> articles;
    private PostItemAdapter articleAdapter;
    private int clickPosition;
    private int INTENT_DETAILS_REQUEST = 1001;

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_penpal_article_list;
    }

    @Override
    protected void initViews() {
        refreshView.setEnableAutoLoadmore(false);
        Bundle bundle = getArguments();
        if (bundle != null) {
            penpalwill_id = bundle.getInt("penpalwill_id");
            type = bundle.getInt("type");
        }
        noData.setBackgroundResource(type == Constant.ARTICLE ? R.drawable.empty_article : R.drawable.empty_post);
        noDataHint.setText(type == Constant.ARTICLE ? "还没有文章哦" : "还没有帖子哦");
    }

    @Override
    protected void initData() {
        setPresenter(new PenpalArticleListPresenter(getActivity()), this);
        articles = new ArrayList<>();
        LinearLayoutManager articleManager = new LinearLayoutManager(getActivity());
        articleList.setLayoutManager(articleManager);
        articleAdapter = new PostItemAdapter(getActivity(), articles);
        articleList.setAdapter(articleAdapter);
        articleAdapter.setAdapterListener(new AdapterListener() {
            @Override
            public void callBack(Object object) {
                if (object instanceof Integer) {
                    clickPosition = (int) object;
                    PenpalArticleResponse.ResponseBean bean = articles.get(clickPosition);
                    if (type == 1) {
                        Intent intent = new Intent(getActivity(), PostDetailsActivity.class);
                        intent.putExtra(Constant.POST_ID, bean.getTopic_id());
                        intent.putExtra("showJoinIcon", true);
                        startActivityForResult(intent, INTENT_DETAILS_REQUEST);
                    } else {
                        Intent intent = new Intent(getActivity(), SingleArticleDetailsActivity.class);
                        ArticleDetaisRequest request = new ArticleDetaisRequest();
                        request.setTopic_id(bean.getTopic_id());
                        request.setContribute_id(bean.getContribute_id());
                        request.setType(3);
                        intent.putExtra(Constant.ARTICLE_DETAILS_REQUEST, request);
                        startActivityForResult(intent, INTENT_DETAILS_REQUEST);
                    }
                }
            }
        });
        pageIndex = 1;
        presenter.getPenpalArticles(penpalwill_id, type, pageIndex, pageSize);
        refreshView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageIndex = 1;
                refreshlayout.resetNoMoreData();
                presenter.getPenpalArticles(penpalwill_id, type, pageIndex, pageSize);
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        refreshView.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageIndex++;
                presenter.getPenpalArticles(penpalwill_id, type, pageIndex, pageSize);
                refreshlayout.finishLoadmore(2000/*,false*/);//传入false表示刷新失败
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {
    }

    @Override
    public void success(Object object, int tag) {
        PenpalArticleResponse response = (PenpalArticleResponse) object;
        if (response != null) {

        }
        if (pageIndex == 1) {
            articles.clear();
        }
        if (refreshView.isRefreshing()) {
            refreshView.finishRefresh();
        }
        if (refreshView.isLoading()) {
            refreshView.finishLoadmore();
        }
        articles.addAll(response.getResponse());
        articleAdapter.notifyDataSetChanged();
        contentIsEmpty(articles.size() == 0);
        if (response.getResponse().size() == 0) {
            refreshView.setLoadmoreFinished(true);
        }
    }

    @Override
    public void faild(Object object, int tag) {

    }

    private void contentIsEmpty(boolean isEmpty) {
        noData.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
        noDataHint.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == INTENT_DETAILS_REQUEST) {
            int likeCount = data.getIntExtra(Constant.LIKE_COUNT, 0);
            int commentCount = data.getIntExtra(Constant.COMMENT_COUNT, 0);
            articles.get(clickPosition).setLikeCount(likeCount);
            articles.get(clickPosition).setCommentCount(commentCount);
            articleAdapter.notifyItemChanged(clickPosition);
        }
    }
}
